from rest_framework.decorators import api_view, authentication_classes, permission_classes
from libs.simple_responses import api_success
from libs.decorators import required_data_fields
from account.services import user_service


@api_view(['POST'])
@authentication_classes([])
@permission_classes([])
@required_data_fields(['username', 'password'])
def login(request):
    username = request.data.get('username')
    password = request.data.get('password')
    _, token = user_service.login(username, password)
    res = {
        'token': token,
    }
    return api_success(res)


@api_view(['POST'])
@permission_classes([])
def logout(request):
    if request.auth:
        user_service.logout(request.auth)
    return api_success({})
